import 'package:intl/intl.dart';

class User {
  final String nick;
  final String avatar;
  final String token;
  final String sex; // 'male' 或 'female'
  final DateTime birthday;

  User({
    required this.nick,
    required this.avatar,
    required this.token,
    required this.sex,
    required this.birthday,
  });

  // 从JSON创建User对象
  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      nick: json['nick'] ?? '',
      avatar: json['avatar'] ?? '',
      token: json['token'] ?? '',
      sex: json['sex'] ?? 'male',
      birthday: json['birthday'] != null
          ? DateTime.parse(json['birthday'])
          : DateTime.now(),
    );
  }

  // 将User对象转换为JSON
  Map<String, dynamic> toJson() {
    final DateFormat formatter = DateFormat('yyyy-MM-dd');
    return {
      'nick': nick,
      'avatar': avatar,
      'token': token,
      'sex': sex,
      'birthday': formatter.format(birthday),
    };
  }

  // 创建User对象的副本
  User copyWith({
    String? nick,
    String? avatar,
    String? token,
    String? sex,
    DateTime? birthday,
  }) {
    return User(
      nick: nick ?? this.nick,
      avatar: avatar ?? this.avatar,
      token: token ?? this.token,
      sex: sex ?? this.sex,
      birthday: birthday ?? this.birthday,
    );
  }
}
